/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class SucursalDAOImpl implements orm.dao.SucursalDAO {
	public Sucursal loadSucursalByORMID(Integer idSucursal) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadSucursalByORMID(session, idSucursal);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal getSucursalByORMID(Integer idSucursal) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return getSucursalByORMID(session, idSucursal);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal loadSucursalByORMID(Integer idSucursal, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadSucursalByORMID(session, idSucursal, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal getSucursalByORMID(Integer idSucursal, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return getSucursalByORMID(session, idSucursal, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal loadSucursalByORMID(PersistentSession session, Integer idSucursal) throws PersistentException {
		try {
			return (Sucursal) session.load(orm.Sucursal.class, idSucursal);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal getSucursalByORMID(PersistentSession session, Integer idSucursal) throws PersistentException {
		try {
			return (Sucursal) session.get(orm.Sucursal.class, idSucursal);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal loadSucursalByORMID(PersistentSession session, Integer idSucursal, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Sucursal) session.load(orm.Sucursal.class, idSucursal, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal getSucursalByORMID(PersistentSession session, Integer idSucursal, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Sucursal) session.get(orm.Sucursal.class, idSucursal, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal[] listSucursalByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return listSucursalByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal[] listSucursalByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return listSucursalByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal[] listSucursalByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Sucursal as Sucursal");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Sucursal[]) list.toArray(new Sucursal[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal[] listSucursalByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Sucursal as Sucursal");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Sucursal[]) list.toArray(new Sucursal[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal loadSucursalByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadSucursalByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal loadSucursalByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadSucursalByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal loadSucursalByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Sucursal[] sucursals = listSucursalByQuery(session, condition, orderBy);
		if (sucursals != null && sucursals.length > 0)
			return sucursals[0];
		else
			return null;
	}
	
	public Sucursal loadSucursalByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Sucursal[] sucursals = listSucursalByQuery(session, condition, orderBy, lockMode);
		if (sucursals != null && sucursals.length > 0)
			return sucursals[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateSucursalByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return iterateSucursalByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateSucursalByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return iterateSucursalByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateSucursalByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Sucursal as Sucursal");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateSucursalByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Sucursal as Sucursal");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal createSucursal() {
		return new orm.Sucursal();
	}
	
	public boolean save(orm.Sucursal sucursal) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().saveObject(sucursal);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Sucursal sucursal) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().deleteObject(sucursal);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Sucursal sucursal)throws PersistentException {
		try {
			if(sucursal.getTiendaidTienda() != null) {
				sucursal.getTiendaidTienda().sucursal.remove(sucursal);
			}
			
			orm.Producto[] lProductos = sucursal.producto.toArray();
			for(int i = 0; i < lProductos.length; i++) {
				lProductos[i].setSucursalidSucursal(null);
			}
			return delete(sucursal);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Sucursal sucursal, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(sucursal.getTiendaidTienda() != null) {
				sucursal.getTiendaidTienda().sucursal.remove(sucursal);
			}
			
			orm.Producto[] lProductos = sucursal.producto.toArray();
			for(int i = 0; i < lProductos.length; i++) {
				lProductos[i].setSucursalidSucursal(null);
			}
			try {
				session.delete(sucursal);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Sucursal sucursal) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().getSession().refresh(sucursal);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Sucursal sucursal) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().getSession().evict(sucursal);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Sucursal loadSucursalByCriteria(SucursalCriteria sucursalCriteria) {
		Sucursal[] sucursals = listSucursalByCriteria(sucursalCriteria);
		if(sucursals == null || sucursals.length == 0) {
			return null;
		}
		return sucursals[0];
	}
	
	public Sucursal[] listSucursalByCriteria(SucursalCriteria sucursalCriteria) {
		return sucursalCriteria.listSucursal();
	}
}
